#!/bin/bash
set -e

WORKDIR=$(cd `dirname $0`; pwd)
BASE_FILE=${WORKDIR}/logs

SERVICE=base-config-service
LOGS_FILE=${BASE_FILE}/${SERVICE}.log
GC_FILE=${BASE_FILE}/gc_logs

HTTP_PORT=10001
DUBBO_PORT=20001
WAIT_TIME=2
# 限制JVM启动内存
MIN_RESOURCE=512M
MAX_RESOURCE=1024M
# 启动打印日志级别，t1、t2、t3是info，dev是debug
LOG_LEVEL=t1

# 设置环境检查异常退出码
NOT_ENVIRONMENT_ERROR=1
NOT_JARFILE_ERROR=2
NOT_ORDER_ERROR=3

# 检查项数组，添加或者删除
ENV_ARRAY=(SERVICE LOGS_FILE HTTP_PORT DUBBO_PORT WAIT_TIME MIN_RESOURCE MAX_RESOURCE LOG_LEVEL GC_FILE)
echo "[precheck] check the neccessary env to start a app"
for env in ${ENV_ARRAY[@]}
do
    echo -en "$env \t ... "
    if [ ! -z $env ]; then
        echo "ok"
    else
        echo "error"
        echo "$env not found, please check ..."
        exit ${NOT_ENVIRONMENT_ERROR}
    fi
done

echo "[precheck] check the service jar file"
echo -en "jar \t ... "
if [ -f ${WORKDIR}/${SERVICE}.jar ];then
    echo "ok"
else
    echo "error"
    echo "${WORKDIR}/${SERVICE}.jar not found, please check ..."
    exit ${NOT_JARFILE_ERROR}
fi

function APP_START {
    echo "Excellent, precheck step is pass."
    echo "start app now, please wait ... " 
    PID=`ps -ef | grep ${SERVICE} | grep java |grep -v grep| awk '{print $2}'`
    if [ -z ${PID} ];then
        java -server -Xms${MIN_RESOURCE} -Xmx${MAX_RESOURCE} \
        -Dspring.profiles.active=${LOG_LEVEL} -jar ${SERVICE}.jar \
        --server.port=${HTTP_PORT} --spring.profiles.active=${LOG_LEVEL} \
        -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=3 \
        -XX:GCLogFileSize=128M -XX:+PrintGC -XX:+PrintGCDetails \
        -XX:+PrintGCTimeStamps -XX:+PrintTenuringDistribution \
        -XX:HeapDumpPath=${GC_FILE} > : &
    else
        echo "[WARN] app is already started. you mean restart?"
    fi
}

function APP_RESTART {
    echo "Excellent, precheck step is pass."
    echo "restart app now, please wait ..."
    PID=`ps -ef | grep ${SERVICE} | grep java |grep -v grep | awk '{print $2}'`
    kill -9 $PID
    java -server -Xms${MIN_RESOURCE} -Xmx${MAX_RESOURCE} \
    -Dspring.profiles.active=${LOG_LEVEL} -jar ${SERVICE}.jar \
    --server.port=${HTTP_PORT} --spring.profiles.active=${LOG_LEVEL} \
    -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=3 \
    -XX:GCLogFileSize=128M -XX:+PrintGC -XX:+PrintGCDetails \
    -XX:+PrintGCTimeStamps -XX:+PrintTenuringDistribution \
    -XX:HeapDumpPath=${GC_FILE} > : &
    PID=`ps -ef | grep ${SERVICE} | grep java |grep -v grep| awk '{print $2}'`
    if [ ! -z ${PID} ];then
        echo "app restart ... success"
    fi
}

echo "[precheck] check the entrypoint order"
ORDER=$1
echo -en "order \t ... "
if [ -z ${ORDER} ]; then
    echo "error"
    echo "ORDER can't be empty, please add start or restart."
    exit ${NOT_ORDER_ERROR}
elif [ x${ORDER} = xstart ];then
    echo "ok"
    APP_START
elif [ x${ORDER} = xrestart ];then
    echo "ok"
    APP_RESTART
else
    echo "error"
    echo "ORDER can't be found, start or restart is only supported!"
    echo "i am out, see you again!!!"
    exit ${NOT_ORDER_ERROR}
fi

while [ ! -f $LOGS_FILE ]
do
   sleep $WAIT_TIME
done
tail -f $LOGS_FILE